﻿using Sidvall.Serialization;

namespace Sidvall.AuthorizationManager.Workflow
{
	public class WorkItemContext
	{
		public string FilePath { get; set; }
		public ActionType ActionType { get; set; }
		public long AuthorizationScopeId { get; set; }
		public bool IncludeResourceItems { get; set; }
		public bool IncludeUserItems { get; set; }
		public bool CloseWindow { get; set; }

		#region GetWorkItemContext

		public static WorkItemContext GetWorkItemContext(string configArgs, string[] args)
		{
			WorkItemContext item;

			item = new WorkItemContext()
			{
				ActionType = ActionType.ExportSecuritySettings,
				CloseWindow = true,
				IncludeResourceItems = false,
				IncludeUserItems = false,
			};
			if ((args == null) || (args.Length < 3))
				args = configArgs.Split(" ", System.StringSplitOptions.RemoveEmptyEntries);
			if ((args != null) && (args.Length >= 3))
			{
				switch (args[0].ToUpperInvariant())
				{
					case "EXPORT":
						item.ActionType = ActionType.ExportSecuritySettings;
						break;
					default:
						throw new System.ArgumentException("use the syntax: Sidvall.AuthorizationManager.Worker <export> [AuthorizationScopeId] [file path] <CloseWindow> <IncludeResourceItems> <IncludeUserItems>");
				}
				item.AuthorizationScopeId = args[1].ToNullableInt64().Value;
				item.FilePath = args[2];
				if (args.Length > 3)
					item.CloseWindow = args[3].ToBoolean(true);
				if (args.Length > 4)
					item.IncludeResourceItems = args[4].ToBoolean(true);
				if (args.Length > 5)
					item.IncludeUserItems = args[5].ToBoolean(true);
			}
			else
				throw new System.ArgumentException("use the syntax: Sidvall.AuthorizationManager.Worker <export> [AuthorizationScopeId] [file path] <CloseWindow> <IncludeResourceItems> <IncludeUserItems>");
			return item;
		}

		#endregion
	}
}
